//
//  ForgetPassWordModel.m
//  baokaodaxue
//
//  Created by liuting on 15/6/14.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "ForgetPassWordModel.h"
#import <AFNetworking.h>

@implementation ForgetPassWordModel

+ (void)forgetByPhone:(NSString *)phone password:(NSString *)password yanzhen:(NSString *)yanzhen success:(void (^)())success failure:(void (^)(NSString *))failure
{
    NSURL *url = [NSURL URLWithString:@"http://www.baokaodaxue.com/passport/password/updateq"];
    
    NSString *post = [NSString stringWithFormat:@"phone=%@&pwd=%@&verifycode=%@",phone,password,yanzhen];
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
    [request setTimeoutInterval:10.0];
    
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
       if (error) {
           NSLog(@"Httperror:%@%ld", error.localizedDescription,(long)error.code);
       } else {
           NSDictionary *responseDic=[NSJSONSerialization JSONObjectWithData:data
                                                                     options:NSJSONReadingMutableLeaves
                                                                       error:nil];
           NSLog(@"%@",responseDic);
           if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
               success();
           }else{
               NSString *messge = [responseDic objectForKey:@"message"];
               failure(messge);
           }
       }
    }];
    

}

+ (void)getYanzhenByPhone:(NSString *)phone success:(void (^)(NSString *))success failure:(void (^)(NSString *))failure
{
    
    
    NSString *get = [NSString stringWithFormat:@"http://www.baokaodaxue.com/passport/captcha/send?phone=%@",phone];
    NSURL *url = [NSURL URLWithString:get];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    [request setTimeoutInterval:10.0];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         if (error) {
             NSLog(@"Httperror:%@%ld", error.localizedDescription,(long)error.code);
         } else {
             NSDictionary *responseDic=[NSJSONSerialization JSONObjectWithData:data
                                                                       options:NSJSONReadingMutableLeaves
                                                                         error:nil];
             NSLog(@"%@",responseDic);
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 success(responseDic[@"extraData"]);
             }else{
                 NSString *messge = [responseDic objectForKey:@"message"];
                 failure(messge);
             }
         }
     }];
}

@end
